home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Heightmap4.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  5.5 KB  |  162 lines

  1. ' Heightmap demo 4
  2. '
  3. ' Vertex lighting
  4. '
  5. const heightScale# = 10 ' Height map scale factor
  6. const texScale# = 0.1   ' Number of textures per grid square
  7. dim xSize, ySize
  8. dim xMask, yMask        ' bitmasks to apply to X and Y coordinates
  9. dim file                ' File handle
  10. dim x, y                ' Working variables
  11. dim angle#              ' Viewing angle (used for animation)
  12. dim texture             ' OpenGL texture handle for our texture
  13. dim tx#, ty#            ' Texture coordinates
  14. dim a#(2), b#(2), c#(2), d#(2), norm#(2) ' Temporary vectors used for lighting
  15. dim intensity#
  16.  
  17. goto Start
  18.  
  19. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  20. ' Routines
  21. CheckError:
  22.     ' Check for file error
  23.     if FileError () <> "" then
  24.         print FileError ()
  25.         CloseFile (file)
  26.         end
  27.     endif
  28.     return
  29.  
  30. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  31. ' Main program stars here    
  32. Start:
  33.  
  34. ' Open file
  35. file = OpenFileRead ("Files\Hills01.dat")
  36.     ' Note: Basic4GL will only open files in the "files" subfolder of the folder
  37.     '       where the program is saved.
  38. gosub CheckError
  39.  
  40. ' Read X and Y size
  41. xSize = Val (ReadLine (file))
  42. ySize = Val (ReadLine (file))
  43. gosub CheckError
  44.  
  45. ' Now that size is known, we can dim our 2D array
  46. dim h#(xSize - 1)(ySize - 1)        
  47. xMask = xSize - 1
  48. yMask = ySize - 1
  49.  
  50. for y = 0 to ySize - 1
  51.     for x = 0 to xSize - 1
  52.         h#(x)(y) = val (ReadText (file, true)) * heightScale#
  53.         gosub CheckError
  54.     next
  55. next          
  56. CloseFile (file)
  57.  
  58. ' Create light map
  59. ' This will be a 2D grid stating the light intensity at each heightmap point, as a
  60. ' value between 0 (fully dark) to 1 (fully lit)
  61. ' To calculate the light map, we need to have a light source, to define the direction
  62. ' of the light.
  63. dim lightSource#(2)
  64. lightSource# = vec3(0, -1, 0)           ' Light shines straight down.
  65. ' (Try changing the light source direction for different effects).
  66.  
  67. lightSource# = Normalize (lightSource#) ' Light source must be length 1
  68.  
  69. ' Create light map
  70. dim l# (xSize - 1)(ySize - 1)
  71. for x = 0 to xSize - 1
  72.     for y = 0 to ySize - 1
  73.         ' A . B
  74.         ' . P .
  75.         ' C . D
  76.         
  77.         ' To calculate the lighting at point P, we cross product the vectors (D-A)
  78.         ' and (B-C) and normalize, to give the normal at point P.
  79.         ' We then dot product the result with the negated light vector.
  80.         ' This will give us a value where 1 = light shining directly onto surface,
  81.         ' and 0 (or below) = surface pointing away from the light.
  82.         ' We raise it to the power of 3 to give a slightly specular effect, and
  83.         ' clamp it at 0.2 to apply an ambient light level.
  84.         a# = vec3 (x,   h#(x)(y),                             y)
  85.         b# = vec3 (x+1, h#((x+1) and xMask)(y),               y)
  86.         c# = vec3 (x  , h#(x              )((y+1) and yMask), y+1)        
  87.         d# = vec3 (x+1, h#((x+1) and xMask)((y+1) and yMask), y+1)
  88.  
  89.         norm# = Normalize (CrossProduct (d# - a#, b# - c#))        
  90.  
  91.         intensity# = norm# * -lightSource#
  92.  
  93.         intensity# = intensity# * intensity# * intensity#
  94.         if intensity# < 0.2 then intensity# = 0.2 endif
  95.             
  96.         l#(x)(y) = intensity#
  97.     next
  98. next
  99.  
  100. ' Load texture file 
  101. texture = LoadMipmapTexture ("textures\00005.jpg")
  102.  
  103. ' Enable texture mapping, and bind our texture
  104. glEnable (GL_TEXTURE_2D)
  105. glBindTexture (GL_TEXTURE_2D, texture)
  106.  
  107. while true
  108.     
  109.     ' Clear the screen
  110.     glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  111.         
  112.     ' Setup the transformations
  113.     glLoadIdentity ()                       ' Clear out any existing transformations
  114.     glTranslatef (0, 0, -50)                ' "push" map away
  115.     glRotatef (20, 1, 0, 0)                 ' Elevation rotation
  116.     glRotatef (angle#, 0, 1, 0)             ' Animation rotation
  117.     glTranslatef (-xSize/2, 0, -ySize/2)    ' Centre the map    
  118.  
  119.     ' Draw the heightmap points
  120.     for x = 0 to xSize - 2
  121.         for y = 0 to ySize - 2
  122.         
  123.             ' Calculate the texture coordinates
  124.             tx# = x * texScale#
  125.             ty# = y * texScale#
  126.  
  127.             ' Draw a grid square.
  128.             ' Apply the lightmap, by setting the colour at each corner.
  129.             ' We will multiply the colour white (1,1,1) as a vector by the light
  130.             ' intensity. Then use the resulting vector as our colour coordinates
  131.             glBegin (GL_QUADS)
  132.                 intensity# = l#(x)(y)
  133.                 glColor3f (intensity#, intensity#, intensity#)
  134.                 glTexCoord2f (tx#            , ty#)
  135.                 glVertex3f (x  , h#(x  )(y  ), y  )
  136.                 
  137.                 intensity# = l#(x+1)(y)
  138.                 glColor3f (intensity#, intensity#, intensity#)
  139.                 glTexCoord2f (tx# + texScale#, ty#)
  140.                 glVertex3f (x+1, h#(x+1)(y  ), y  )
  141.  
  142.                 intensity# = l#(x+1)(y+1)
  143.                 glColor3f (intensity#, intensity#, intensity#)
  144.                 glTexCoord2f (tx# + texScale#, ty# + texScale#)
  145.                 glVertex3f (x+1, h#(x+1)(y+1), y+1)
  146.  
  147.                 intensity# = l#(x)(y+1)
  148.                 glColor3f (intensity#, intensity#, intensity#)
  149.                 glTexCoord2f (tx#            , ty# + texScale#)
  150.                 glVertex3f (x  , h#(x  )(y+1), y+1)
  151.             glEnd ()
  152.         next
  153.     next
  154.     
  155.     ' Show the result
  156.     SwapBuffers ()
  157.     
  158.     ' Animation
  159.     while SyncTimer (20)
  160.         angle# = angle# + 0.8
  161.     wend
  162. wend